/*
 * XmlUtils.h
 *
 *  Created on: 13-11-2011
 *      Author: cristian
 */

#ifndef XMLUTILS_H_
#define XMLUTILS_H_

#define TIXML_USE_STL
#include "tinyxml.h" //http://www.grinninglizard.com/tinyxmldocs/tutorial0.html
#include "string"
#include "list"
#include "string"

class XmlUtils {
public:
	XmlUtils();
	virtual ~XmlUtils();
	static string* statusServerXML(string nombre, unsigned int id, int workersSize, string* ip);
	static string* statusClientXML();
	static string* createServersXml(list<string>* server);
	static list<string*>* loadrssFromFile(string file);

};

inline string *XmlUtils::statusServerXML(string nombre, unsigned int id, int workersSize, string* ip) {

	stringstream st;
	st << id;
	string _id = st.str();

	stringstream st2;
	st2 << workersSize;
	string _size = st2.str();

	TiXmlDocument doc;
	TiXmlElement* msg;
	TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
	doc.LinkEndChild(decl);

	TiXmlElement * root = new TiXmlElement("Hal");
	doc.LinkEndChild(root);

	TiXmlComment * comment = new TiXmlComment();
	comment->SetValue(" Message file for Hal ");
	root->LinkEndChild(comment);

	TiXmlElement * msgs = new TiXmlElement("Server");
	root->LinkEndChild(msgs);

	msg = new TiXmlElement("Nombre");
	msg->LinkEndChild(new TiXmlText(nombre));
	msgs->LinkEndChild(msg);

	msg = new TiXmlElement("Id");
	msg->LinkEndChild(new TiXmlText(_id));
	msgs->LinkEndChild(msg);

	msg = new TiXmlElement("ip");
	msg->LinkEndChild(new TiXmlText(*ip));
	msgs->LinkEndChild(msg);

	msg = new TiXmlElement("WorkersSize");
	msg->LinkEndChild(new TiXmlText(_size));
	msgs->LinkEndChild(msg);

	//	TiXmlElement * windows = new TiXmlElement( "Windows" );
	//	root->LinkEndChild( windows );

	//	TiXmlElement * window;
	//	window = new TiXmlElement( "Window" );
	//	windows->LinkEndChild( window );
	//	window->SetAttribute("name", "MainFrame");
	//	window->SetAttribute("x", 5);
	//	window->SetAttribute("y", 15);
	//	window->SetAttribute("w", 400);
	//	window->SetAttribute("h", 250);

//		TiXmlElement * cxn = new TiXmlElement( "Tipo" );
//		root->LinkEndChild( cxn );
//		cxn->SetAttribute("ip", "192.168.0.1");
//		cxn->SetAttribute("Tipo", "TEXT");

	string* text = new string();
	*text << doc;
	return text;
	//doc.SaveFile( file );
}

inline string *XmlUtils::statusClientXML() {
	TiXmlDocument doc;
	TiXmlElement* msg;

	TiXmlElement * root = new TiXmlElement("Hal");
	doc.LinkEndChild(root);

	TiXmlComment * comment = new TiXmlComment();
	comment->SetValue(" Message file for Hal ");
	root->LinkEndChild(comment);

	TiXmlElement * msgs = new TiXmlElement("Cliente");
	root->LinkEndChild(msgs);

	msg = new TiXmlElement("Nombre");
	msg->LinkEndChild(new TiXmlText("Nombre del cliente"));
	msgs->LinkEndChild(msg);

	msg = new TiXmlElement("Id");
	msg->LinkEndChild(new TiXmlText("Id del cliente"));
	msgs->LinkEndChild(msg);

	string* text = new string();
	*text << doc;
	return text;
	//doc.SaveFile( file );
}

inline string *XmlUtils::createServersXml(list<string>* server) {

	string* servers = new string;

	list<string>::iterator pos;
	pos = server->begin();

	while (pos != server->end()) {
		(*servers) += *pos;
		(*servers) += "\n";
		pos++;
	}

	return servers;
}

inline list<string*> *XmlUtils::loadrssFromFile(string file) {
	TiXmlDocument doc(file);
	bool loadOkay = doc.LoadFile();

	list<string*>* listarss = new list<string*>;


	if (loadOkay) {
		TiXmlHandle hDoc(&doc);
		TiXmlElement* pElem;
		TiXmlHandle hRoot(0);

		pElem = hDoc.FirstChildElement().Element();

		if (!pElem)
			return NULL;

		hRoot = TiXmlHandle(pElem);

		TiXmlElement* pWindowNode = hRoot.FirstChild("body").FirstChild().Element();
		for (pWindowNode; pWindowNode; pWindowNode = pWindowNode->NextSiblingElement()) {

			const char *pName = pWindowNode->Attribute("xmlUrl");


			if (pName != NULL){
				string* rss = new string(pName);
				listarss->push_back(rss);
			}


		//	printf("%s \n", pName);

		}

		//printf("File \n%s:\n", file.c_str());
	} else {
		printf("Failed to load file \"%s\"\n", file.c_str());
	}
	return listarss;
}

#endif /* XMLUTILS_H_ */
